package com.yidu.hbatsft.action;

import com.yidu.hbatsft.domain.AtsBaseSport;
import com.yidu.hbatsft.domain.AtsEmpCoach;
import com.yidu.hbatsft.domain.AtsEmpCoachAndUserVo;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/AtsEmpCoachAction")
public class AtsEmpCoachController {
     @Resource
     AtsEmpCoachService service;

    /**
     *查询所有的方法
     * @param
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAlltwo(){
        System.out.println("进入查询所有方法two");
        List<AtsEmpCoach> list =service.findAll();

        Map<String, Object> map=new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    @RequestMapping("selectAllList")
    @ResponseBody
    public List<AtsEmpCoach> selectAllList(){
        System.out.println("lllllllllllllllllll");
        List<AtsEmpCoach> list =service.findAll();
        return list;
    }

    @RequestMapping("findAllX")
    @ResponseBody
    public Map<String, Object> findAllX(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize,String athleteName){
        List<AtsEmpCoach> list = service.findAllX(pageNum,pageSize,athleteName);

        Map<String, Object> map=new HashMap<String, Object>();

        int cout = service.count();
        map.put("code",0);
        map.put("msg","");
        map.put("count",cout);
        map.put("data",list);
        return map;
    }
    /**
     * 根据ID查询教练员
     * AtsEmpCoachAndUserVo 关联类
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsEmpCoachAndUserVo findById(Integer id){
        System.out.println("进入教练ID查询");
        System.out.println(id);
        AtsEmpCoachAndUserVo vo = service.selectById(id);
        if(vo!=null){
            System.out.println("用户ID："+vo.getUsName()+"教练ID："+vo.getCoachName()+"项目ID:"+vo.getSportId());

            return vo;
        }else{
            System.out.println("没有查到");
            return null;
        }


    }
    /**
     * 文件上传新增和修改
     */
    @RequestMapping("userInfo")
    @ResponseBody
    public Message userInfo(AtsEmpCoachAndUserVo userVo,MultipartFile file){
        Message msg = new Message();

        //System.err.println(file+"=============================qeqeqe");
        int count = service.upOrInsertUserCoachDate(userVo,file);
        System.err.println(userVo.getCoachId()+"控制器的教练ID");
        if(count>0){
            msg.setMsg("操作成功");
            msg.setNum(1);
            return msg;
        }else{
            msg.setMsg("操作异常");
            msg.setNum(0);
            return msg;
        }

    }
    /**
     * 加载专业选项
     */
    @RequestMapping("loadSport")
    @ResponseBody
    public List<AtsBaseSport> loadSportName(){
        List<AtsBaseSport> sport = service.loadSportName();
        /*for (AtsBaseSport atsBaseSport : sport) {
            System.err.println("专业ID："+atsBaseSport.getSportId());
        }*/
        return  sport;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteCoachDates")
    @ResponseBody
    public Message deleteCoachDates(String ids){
        String id[] = ids.split(",");
        int cout =0;
        for (int i=0;i<id.length;i++){

            //System.out.println(id[i]);
            //将类型转换成int
            Integer e = Integer.valueOf(id[i]);
            System.err.println(e);
            cout = service.deleteById(e);
        }
        Message msg = new Message();
        if(cout >0){
            msg.setMsg("删除成功");
        }else{
            msg.setMsg("删除异常");
        }
        return msg;
    }

    @RequestMapping("deleteCoachDate")
    @ResponseBody
    public Message deleteCoachDate(Integer id){
       int cout = service.deleteById(id);
       Message msg = new Message();
        if(cout >0){
            msg.setMsg("删除成功！");
        }else {
            msg.setMsg("删除失败");
        }
        return  msg;
    }
}
